package com.cloudlead.fleetAction.service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.common.worker.client.exception.RestClientException;
import com.cloudlead.commonData.service.*;
import com.cloudlead.commonData.vo.*;
import com.cloudlead.fleetAction.service.BaseCarEditSer;
import com.cloudlead.fleetService.vo.BaseCarVo;
import com.cloudlead.fleetService.vo.BaseGpsVo;
import com.cloudlead.fleetService.vo.TbCarOperationVo;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 20150817 on 2015/9/1.
 */
@Service
public class BaseCarEditSerImpl implements BaseCarEditSer{
    @Autowired
    BaseCarService baseCarService;
    @Autowired
    BaseCompanyService baseCompanyService;
    @Autowired
    BaseDriverService baseDriverService;
    @Autowired
    TbFuelRegisterService tbFuelRegisterService;
    @Autowired
    BaseGpsService baseGpsService;
    @Autowired
    TbCarOperationService tbCarOperationService;

    //创建车辆
    @Override
    public void createCar(BaseCarVo vo) {
        BaseCarVo qryVo=new BaseCarVo();
        qryVo.setBaseCompany(vo.getBaseCompany());
        qryVo.setLicencePlate(vo.getLicencePlate());
        Page p=findAllCar(qryVo, 1, 1);
        if (p.getContent().size()>0){
            if(((BaseCar)p.getContent().get(0)).getFlag().equals("1"))
                throw new RestClientException("车牌号\""+vo.getLicencePlate()+"\"已存在。该车辆停用状态，请恢复");
            else  throw new RestClientException("车牌号\""+vo.getLicencePlate()+"\"已存在。");

        }
        BaseCar bc = new BaseCar();
        BeanUtils.copyProperties(vo,bc);

        if(vo.getGps()!=null&&!vo.getGps().equals("")){//GPS
            BaseGps baseGps = new BaseGps();
            baseGps.setId(vo.getGps());
            bc.setBaseG(baseGps);
        }
        if(vo.getBaseCompany()!=null&&!vo.getBaseCompany().equals("")){//如果公司不为空
            BaseCompany bcy = new BaseCompany();
            bcy.setId(vo.getBaseCompany());
            bc.setBaseCompany(bcy);
        }
        if(vo.getBaseDriver()!=null&&!vo.getBaseDriver().equals("")){//如果BaseDriver不为空
            BaseDriver bd = new BaseDriver();
            bd.setId(vo.getBaseDriver());
            bc.setBaseDriver(bd);
        }
        if(vo.getTbFuelRegister()!=null&&!vo.getTbFuelRegister().equals("")){//如果TbFuelRegister不为空
            TbFuelRegister tr = new TbFuelRegister();
            tr.setId(vo.getTbFuelRegister());
            bc.setTbFuelRegister(tr);
        }
        bc.setComments(vo.getComments());
        bc.setFlag("0");//flag初始为0
        bc.setCompany(vo.getCompany());
        bc.setDeadWeight(vo.getDeadWeight());
        bc.setEngineNumber(vo.getEngineNumber());
        if(vo.getFleetId() !=null&&!vo.getFleetId().equals("")){
            BaseFleet baseFleet = new BaseFleet();
            baseFleet.setId(vo.getFleetId());
            bc.setBaseFleet(baseFleet);
        }
        bc.setFrameNumber(vo.getFrameNumber());
        bc.setInsurancePolicy(vo.getInsurancePolicy());
        bc.setLiabilityInsura(vo.getLiabilityInsura());
        bc.setWeixinNo(vo.getWeixinNo());
        bc.setWeixinPassword(vo.getWeixinPassword());
        bc.setStatus("69");
        baseCarService.create(bc);
    }

    //编辑车辆
    @Override
    public void editCar(BaseCarVo vo) {
        BaseCarVo qryVo=new BaseCarVo();
        qryVo.setBaseCompany(vo.getBaseCompany());
        qryVo.setLicencePlate(vo.getLicencePlate());
        Page p=findAllCar(qryVo, 1, 1);
        if (p.getContent().size()>0 && !((BaseCar)p.getContent().get(0)).getId().equals(vo.getId())){
            if(((BaseCar)p.getContent().get(0)).getFlag().equals("1"))
                throw new RestClientException("车牌号\""+vo.getLicencePlate()+"\"已存在。该车辆停用状态，请恢复");
            else  throw new RestClientException("车牌号\""+vo.getLicencePlate()+"\"已存在。");
        }

        BaseCar bc = baseCarService.view(vo.getId());
        BeanUtils.copyProperties(vo,bc);
        if(vo.getGps()!=null&&!vo.getGps().equals("")){//GPS
            BaseGps baseGps = new BaseGps();
            baseGps.setId(vo.getGps());
            bc.setBaseG(baseGps);
        }
        if(vo.getBaseCompany()!=null&&!vo.getBaseCompany().equals("")){//如果公司不为空
            BaseCompany bcy = new BaseCompany();
            bcy.setId(vo.getBaseCompany());
            bc.setBaseCompany(bcy);
        }
        if(vo.getBaseDriver()!=null&&!vo.getBaseDriver().equals("")){//如果BaseDriver不为空
            BaseDriver bd = new BaseDriver();
            bd.setId(vo.getBaseDriver());
            bc.setBaseDriver(bd);
        }
        if(vo.getTbFuelRegister()!=null&&!vo.getTbFuelRegister().equals("")){//如果TbFuelRegister不为空
            TbFuelRegister tr = new TbFuelRegister();
            tr.setId(vo.getTbFuelRegister());
            bc.setTbFuelRegister(tr);
        }
        bc.setComments(vo.getComments());
        bc.setFlag("0");
        bc.setCompany(vo.getCompany());
        bc.setDeadWeight(vo.getDeadWeight());
        bc.setDriver(vo.getDriver());
        bc.setEngineNumber(vo.getEngineNumber());
        if(vo.getFleetId() !=null&&!vo.getFleetId().equals("")){
            BaseFleet baseFleet = new BaseFleet();
            baseFleet.setId(vo.getFleetId());
            bc.setBaseFleet(baseFleet);
        }
        bc.setFrameNumber(vo.getFrameNumber());
        bc.setInsurancePolicy(vo.getInsurancePolicy());
        bc.setLiabilityInsura(vo.getLiabilityInsura());
        bc.setWeixinNo(vo.getWeixinNo());
        bc.setWeixinPassword(vo.getWeixinPassword());
        baseCarService.update(bc);
    }

    //单值查询
    @Override
    public Object findOneCar(BaseCarVo vo) {
        BaseCar bc = new BaseCar();
        BeanUtils.copyProperties(vo,bc);
        bc.setFlag("0");
        BaseCar bc1 = baseCarService.findOne(bc);//获取数据库的值
        return bc1;
    }

    //分页查询
    @Override
    public Page<BaseCarVo> findAllCar(BaseCarVo vo, int page, int size) {
        BaseCarVo bco = new BaseCarVo();
        if(vo!=null){//如果vo不为空
            bco=vo;
        }
        PageRequest pr = new PageRequest();
        List<Term> tr = new ArrayList<Term>();
        if(bco.getFlag()!=null&&!bco.getFlag().equals("")){
            tr.add(new Term("flag",bco.getFlag()));//未删除或已删除
        }
        //if(bco.getBaseCompany()!=null&&!bco.getBaseCompany().equals("")){//如果公司不为空
        tr.add(new Term("baseCompanyId",bco.getBaseCompany()));
        //}
        if(bco.getCompany()!=null&&!bco.getCompany().equals("")){
            tr.add(new Term("company",Operator.LK,bco.getCompany()));
        }
        if(bco.getDriver()!=null&&!bco.getDriver().equals("")){
            tr.add(new Term("baseDriver.id",Operator.LK,bco.getDriver()));
        }
        if(bco.getBaseDriver()!=null&&!bco.getBaseDriver().equals(""))
            tr.add(new Term("baseDriver.id",bco.getBaseDriver()));
        if(bco.getLicencePlate()!=null&&!bco.getLicencePlate().equals("")){
            tr.add(new Term("licencePlate",Operator.LK,bco.getLicencePlate()));
        }
        Filter filter = new Filter(new Criteria(tr));
        pr.setPage(page);
        pr.setSize(size);
        pr.setFilter(filter);
        Page<BaseCar> pb = (Page<BaseCar>)baseCarService.findAll(pr);
        Page<BaseCarVo> pbv = new Page<BaseCarVo>();
        BeanUtils.copyProperties(pb,pbv);
        return pbv;
    }

    //删除车辆
    @Override
    @Transactional//回滚
    public void delCar(String id) {
        BaseCar bc = baseCarService.view(id);//根据ID删除
        bc.setFlag("1");
        baseCarService.update(bc);
    }

    @Override
    @Transactional//回滚
    public void hfCar(String id) {
        BaseCar bc = baseCarService.view(id);//根据ID恢复
        bc.setFlag("0");
        baseCarService.update(bc);
    }

    @Override
    public void relieveWX(String id) {
        BaseCar bc = baseCarService.view(id);//
        bc.setWeixinNo(null);
        bc.setWeixinPassword(null);
        bc.setFlag("0");//flag初始为0
        baseCarService.update(bc);
    }

    @Override
    public Page<BaseGpsVo> finAllGps(BaseGpsVo baseGpsVo, int page, int size) {
        BaseGpsVo qryBaseGpsVo = new BaseGpsVo();
        if(baseGpsVo!=null){//如果baseGpsVo不为空
            qryBaseGpsVo=baseGpsVo;
        }
        PageRequest pr = new PageRequest();
        List<Term> tr = new ArrayList<Term>();
        if(qryBaseGpsVo.getFleetId()!=null&&!qryBaseGpsVo.getFleetId().equals("")){
            tr.add(new Term("fleet.id",qryBaseGpsVo.getFleetId()));
        }
        Filter filter = new Filter(new Criteria(tr));
        pr.setPage(page);
        pr.setSize(size);
        pr.setFilter(filter);
        Page<BaseGps> pb = (Page<BaseGps>)baseGpsService.findAll(pr);
        Page<BaseGpsVo> pbv = new Page<BaseGpsVo>();
        BeanUtils.copyProperties(pb,pbv);
        return pbv;
    }

    @Override
    public Page<TbCarOperationVo> findAllCarS(TbCarOperationVo vo, int page, int size) {
        TbCarOperationVo tco = new TbCarOperationVo();
        if(vo!=null){//如果vo不为空
            tco=vo;
        }
        PageRequest pr = new PageRequest();
        List<Term> tr = new ArrayList<Term>();
        if(tco.getCar()!=null&&!tco.getCar().equals("")){
            tr.add(new Term("car.id",tco.getCar()));//车辆ID
        }
        Sort sort = new Sort();//创建按发送时间倒序排列
        if(vo!=null) {
            if (!"".equals(vo.getSort()) && vo.getSort() != null && !"".equals(vo.getOrder()) && vo.getOrder() != null) {
                sort.add(new Sort.Order(vo.getOrder().equals("desc") ? Sort.Direction.DESC : Sort.Direction.ASC, vo.getSort()));
            }
        }
        Filter filter = new Filter(new Criteria(tr));
        pr.setPage(page);
        pr.setSize(size);
        pr.setSort(sort);
        pr.setFilter(filter);
        Page<TbCarOperation> pb = (Page<TbCarOperation>)tbCarOperationService.findAll(pr);
        Page<TbCarOperationVo> pbv = new Page<TbCarOperationVo>();
        BeanUtils.copyProperties(pb,pbv);
        return pbv;
    }

    @Override
    public void editCarStatus(BaseCarVo vo) {
        BaseCar baseCar = baseCarService.view(vo.getId());
        baseCar.setStatus(vo.getStatus());
        baseCarService.update(baseCar);
        TbCarOperation tbCarOperation = new TbCarOperation();
        tbCarOperation.setCar(baseCar);
        tbCarOperation.setCreateBy(vo.getCreateBy());
        tbCarOperation.setAmount(vo.getAmount());
        tbCarOperation.setOperation(vo.getStatus());
        tbCarOperation.setCreateDate(new DateTime());
        tbCarOperationService.create(tbCarOperation);
    }

}
